package com.openbravo.pos.customers;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.QBFCompareEnum;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.data.user.ListProvider;
import com.openbravo.data.user.ListProviderCreator;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.editor.JEditorString;
import com.openbravo.pos.forms.AppLocal;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JCustomerFinder extends JDialog
  implements EditorCreator
{
  private CustomerInfo selectedCustomer;
  private ListProvider lpr;
  private JButton jButton1;
  private JButton jButton3;
  private JLabel jLabel5;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JList jListCustomers;
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JPanel jPanel4;
  private JPanel jPanel5;
  private JPanel jPanel6;
  private JPanel jPanel7;
  private JPanel jPanel8;
  private JScrollPane jScrollPane1;
  private JButton jcmdCancel;
  private JButton jcmdOK;
  private JEditorKeys m_jKeys;
  private JEditorString m_jtxtName;
  private JEditorString m_jtxtSearchKey;
  private JEditorString m_jtxtTaxID;

  private JCustomerFinder(Frame paramFrame, boolean paramBoolean)
  {
    super(paramFrame, paramBoolean);
  }

  private JCustomerFinder(Dialog paramDialog, boolean paramBoolean)
  {
    super(paramDialog, paramBoolean);
  }

  public static JCustomerFinder getCustomerFinder(Component paramComponent, DataLogicCustomers paramDataLogicCustomers)
  {
    Window localWindow = getWindow(paramComponent);
    JCustomerFinder localJCustomerFinder;
    if ((localWindow instanceof Frame))
      localJCustomerFinder = new JCustomerFinder((Frame)localWindow, true);
    else
      localJCustomerFinder = new JCustomerFinder((Dialog)localWindow, true);
    localJCustomerFinder.init(paramDataLogicCustomers);
    localJCustomerFinder.applyComponentOrientation(paramComponent.getComponentOrientation());
    return localJCustomerFinder;
  }

  public CustomerInfo getSelectedCustomer()
  {
    return this.selectedCustomer;
  }

  private void init(DataLogicCustomers paramDataLogicCustomers)
  {
    initComponents();
    this.jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
    this.m_jtxtTaxID.addEditorKeys(this.m_jKeys);
    this.m_jtxtSearchKey.addEditorKeys(this.m_jKeys);
    this.m_jtxtName.addEditorKeys(this.m_jKeys);
    this.m_jtxtTaxID.reset();
    this.m_jtxtSearchKey.reset();
    this.m_jtxtName.reset();
    this.m_jtxtTaxID.activate();
    this.lpr = new ListProviderCreator(paramDataLogicCustomers.getCustomerList(), this);
    this.jListCustomers.setCellRenderer(new CustomerRenderer());
    getRootPane().setDefaultButton(this.jcmdOK);
    this.selectedCustomer = null;
  }

  public void search(CustomerInfo paramCustomerInfo)
  {
    if ((paramCustomerInfo == null) || (paramCustomerInfo.getName() == null) || (paramCustomerInfo.getName().equals("")))
    {
      this.m_jtxtTaxID.reset();
      this.m_jtxtSearchKey.reset();
      this.m_jtxtName.reset();
      this.m_jtxtTaxID.activate();
      cleanSearch();
    }
    else
    {
      this.m_jtxtTaxID.setText(paramCustomerInfo.getTaxid());
      this.m_jtxtSearchKey.setText(paramCustomerInfo.getSearchkey());
      this.m_jtxtName.setText(paramCustomerInfo.getName());
      this.m_jtxtTaxID.activate();
      executeSearch();
    }
  }

  private void cleanSearch()
  {
    this.jListCustomers.setModel(new MyListData(new ArrayList()));
  }

  public void executeSearch()
  {
    try
    {
      this.jListCustomers.setModel(new MyListData(this.lpr.loadData()));
      if (this.jListCustomers.getModel().getSize() > 0)
        this.jListCustomers.setSelectedIndex(0);
    }
    catch (BasicException localBasicException)
    {
      localBasicException.printStackTrace();
    }
  }

  public Object createValue()
    throws BasicException
  {
    Object[] arrayOfObject = new Object[6];
    if ((this.m_jtxtTaxID.getText() == null) || (this.m_jtxtTaxID.getText().equals("")))
    {
      arrayOfObject[0] = QBFCompareEnum.COMP_NONE;
      arrayOfObject[1] = null;
    }
    else
    {
      arrayOfObject[0] = QBFCompareEnum.COMP_RE;
      arrayOfObject[1] = ("%" + this.m_jtxtTaxID.getText() + "%");
    }
    if ((this.m_jtxtSearchKey.getText() == null) || (this.m_jtxtSearchKey.getText().equals("")))
    {
      arrayOfObject[2] = QBFCompareEnum.COMP_NONE;
      arrayOfObject[3] = null;
    }
    else
    {
      arrayOfObject[2] = QBFCompareEnum.COMP_RE;
      arrayOfObject[3] = ("%" + this.m_jtxtSearchKey.getText() + "%");
    }
    if ((this.m_jtxtName.getText() == null) || (this.m_jtxtName.getText().equals("")))
    {
      arrayOfObject[4] = QBFCompareEnum.COMP_NONE;
      arrayOfObject[5] = null;
    }
    else
    {
      arrayOfObject[4] = QBFCompareEnum.COMP_RE;
      arrayOfObject[5] = ("%" + this.m_jtxtName.getText() + "%");
    }
    return arrayOfObject;
  }

  private static Window getWindow(Component paramComponent)
  {
    if (paramComponent == null)
      return new JFrame();
    if (((paramComponent instanceof Frame)) || ((paramComponent instanceof Dialog)))
      return (Window)paramComponent;
    return getWindow(paramComponent.getParent());
  }

  private void initComponents()
  {
    this.jPanel2 = new JPanel();
    this.m_jKeys = new JEditorKeys();
    this.jPanel3 = new JPanel();
    this.jPanel5 = new JPanel();
    this.jPanel7 = new JPanel();
    this.jLabel5 = new JLabel();
    this.m_jtxtName = new JEditorString();
    this.jLabel6 = new JLabel();
    this.m_jtxtSearchKey = new JEditorString();
    this.jLabel7 = new JLabel();
    this.m_jtxtTaxID = new JEditorString();
    this.jPanel6 = new JPanel();
    this.jButton1 = new JButton();
    this.jButton3 = new JButton();
    this.jPanel4 = new JPanel();
    this.jScrollPane1 = new JScrollPane();
    this.jListCustomers = new JList();
    this.jPanel8 = new JPanel();
    this.jPanel1 = new JPanel();
    this.jcmdOK = new JButton();
    this.jcmdCancel = new JButton();
    setDefaultCloseOperation(2);
    setTitle(AppLocal.getIntString("form.customertitle"));
    this.jPanel2.setLayout(new BorderLayout());
    this.jPanel2.add(this.m_jKeys, "North");
    getContentPane().add(this.jPanel2, "After");
    this.jPanel3.setLayout(new BorderLayout());
    this.jPanel5.setLayout(new BorderLayout());
    this.jLabel5.setText(AppLocal.getIntString("label.prodname"));
    this.jLabel6.setText(AppLocal.getIntString("label.searchkey"));
    this.jLabel7.setText(AppLocal.getIntString("label.taxid"));
    GroupLayout localGroupLayout = new GroupLayout(this.jPanel7);
    this.jPanel7.setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel7, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jtxtTaxID, -2, 220, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel5, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jtxtName, -2, 220, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel6, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jtxtSearchKey, -2, 220, -2))).addContainerGap(-1, 32767)));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel7).addComponent(this.m_jtxtTaxID, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel6).addComponent(this.m_jtxtSearchKey, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel5).addComponent(this.m_jtxtName, -2, -1, -2)).addContainerGap(-1, 32767)));
    this.jPanel5.add(this.jPanel7, "Center");
    this.jButton1.setText(AppLocal.getIntString("button.clean"));
    this.jButton1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCustomerFinder.this.jButton1ActionPerformed(paramActionEvent);
      }
    });
    this.jPanel6.add(this.jButton1);
    this.jButton3.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/launch.png")));
    this.jButton3.setText(AppLocal.getIntString("button.executefilter"));
    this.jButton3.setFocusPainted(false);
    this.jButton3.setFocusable(false);
    this.jButton3.setRequestFocusEnabled(false);
    this.jButton3.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCustomerFinder.this.jButton3ActionPerformed(paramActionEvent);
      }
    });
    this.jPanel6.add(this.jButton3);
    this.jPanel5.add(this.jPanel6, "South");
    this.jPanel3.add(this.jPanel5, "First");
    this.jPanel4.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    this.jPanel4.setLayout(new BorderLayout());
    this.jListCustomers.setFocusable(false);
    this.jListCustomers.setRequestFocusEnabled(false);
    this.jListCustomers.addMouseListener(new MouseAdapter()
    {
      public void mouseClicked(MouseEvent paramMouseEvent)
      {
        JCustomerFinder.this.jListCustomersMouseClicked(paramMouseEvent);
      }
    });
    this.jListCustomers.addListSelectionListener(new ListSelectionListener()
    {
      public void valueChanged(ListSelectionEvent paramListSelectionEvent)
      {
        JCustomerFinder.this.jListCustomersValueChanged(paramListSelectionEvent);
      }
    });
    this.jScrollPane1.setViewportView(this.jListCustomers);
    this.jPanel4.add(this.jScrollPane1, "Center");
    this.jPanel3.add(this.jPanel4, "Center");
    this.jPanel8.setLayout(new BorderLayout());
    this.jcmdOK.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png")));
    this.jcmdOK.setText(AppLocal.getIntString("Button.OK"));
    this.jcmdOK.setEnabled(false);
    this.jcmdOK.setFocusPainted(false);
    this.jcmdOK.setFocusable(false);
    this.jcmdOK.setMargin(new Insets(8, 16, 8, 16));
    this.jcmdOK.setRequestFocusEnabled(false);
    this.jcmdOK.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCustomerFinder.this.jcmdOKActionPerformed(paramActionEvent);
      }
    });
    this.jPanel1.add(this.jcmdOK);
    this.jcmdCancel.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png")));
    this.jcmdCancel.setText(AppLocal.getIntString("Button.Cancel"));
    this.jcmdCancel.setFocusPainted(false);
    this.jcmdCancel.setFocusable(false);
    this.jcmdCancel.setMargin(new Insets(8, 16, 8, 16));
    this.jcmdCancel.setRequestFocusEnabled(false);
    this.jcmdCancel.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JCustomerFinder.this.jcmdCancelActionPerformed(paramActionEvent);
      }
    });
    this.jPanel1.add(this.jcmdCancel);
    this.jPanel8.add(this.jPanel1, "After");
    this.jPanel3.add(this.jPanel8, "South");
    getContentPane().add(this.jPanel3, "Center");
    Dimension localDimension = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((localDimension.width - 613) / 2, (localDimension.height - 610) / 2, 613, 610);
  }

  private void jcmdOKActionPerformed(ActionEvent paramActionEvent)
  {
    this.selectedCustomer = ((CustomerInfo)this.jListCustomers.getSelectedValue());
    dispose();
  }

  private void jcmdCancelActionPerformed(ActionEvent paramActionEvent)
  {
    dispose();
  }

  private void jButton3ActionPerformed(ActionEvent paramActionEvent)
  {
    executeSearch();
  }

  private void jListCustomersValueChanged(ListSelectionEvent paramListSelectionEvent)
  {
    this.jcmdOK.setEnabled(this.jListCustomers.getSelectedValue() != null);
  }

  private void jListCustomersMouseClicked(MouseEvent paramMouseEvent)
  {
    if (paramMouseEvent.getClickCount() == 2)
    {
      this.selectedCustomer = ((CustomerInfo)this.jListCustomers.getSelectedValue());
      dispose();
    }
  }

  private void jButton1ActionPerformed(ActionEvent paramActionEvent)
  {
    this.m_jtxtTaxID.reset();
    this.m_jtxtSearchKey.reset();
    this.m_jtxtName.reset();
    this.m_jtxtTaxID.activate();
    cleanSearch();
  }

  private static class MyListData extends AbstractListModel
  {
    private List m_data;

    public MyListData(List paramList)
    {
      this.m_data = paramList;
    }

    public Object getElementAt(int paramInt)
    {
      return this.m_data.get(paramInt);
    }

    public int getSize()
    {
      return this.m_data.size();
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.customers.JCustomerFinder
 * JD-Core Version:    0.6.0
 */